#!/bin/sh
#-------------------------------------------------------------------------+
# Copyright (C) 2016 Matt Churchyard (churchers@gmail.com)
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# load a configuration file
# this reads the specfied file into the global VM_CONFIG variable.
# we have very basic parsing that uses # for comments and requires
# all variables to be at the beginning of the line in lowercase.
# Note also that a # within double quotes will still be treated
# as the start of a comment.
#
# @param string _file full path of the file to read
# @modifies VM_CONFIG
#
config::load(){
    local _file="$1"

    # read config file
    # we kick out any lines that don't start with a letter,
    # scrap anything after a # character, and remove double-quotes
    VM_CONFIG=$(grep '^[a-z]' "${_file}" 2>/dev/null | awk -F# '{print $1}' | sed -e 's@ *$@@' | tr -d '"')
}

# get a configuration value from the current config file
#
# @param string _var the variable to put value into
# @param string _key the name of the config key to retrieve
# @param optional string _def default value to return if setting not found
# @return true if setting found
#
config::get(){
    local _c_var="$1"
    local _c_key="$2"
    local _c_def="$3"
    local _c_line
    local IFS=$'\n'

    for _c_line in ${VM_CONFIG}; do
        if [ "${_c_key}" = "${_c_line%%=*}" ]; then
            setvar "${_c_var}" "${_c_line#*=}"
            return 0
        fi
    done

    # not found
    setvar "${_c_var}" "${_c_def}"
    return 1
}

# simple wrapper to check a config setting to see if it's 
# set to yes/no true/false etc
#
# @param string _key the config key to check
# @param string _def default value if config key doesn't exist
# @return true(0) if set to anything other than no/false/off/0
#
config::yesno(){
    local _key="$1"
    local _def="$2"
    local _value

    config::get "_value" "${_key}" "${_def}"
    util::yesno "${_value}"
}

# set a value in guest configuration file
# we check for newline at the end as sysrc won't add it
# and that will mess up the new key and the existing one
# from the end of the file
#
# @param string _name guest name
# @param string _key config key to set
# @param string _value value
# @param int _skip_newline_check skip the check for newline
# @return true if sysrc successful
#
config::set(){
    local _name="$1"
    local _key="$2"
    local _value="$3"
    local _skip_newline_check="$4"
    local _newline

    if [ -z "${_skip_newline_check}" ]; then
        _newline=$(tail -1 "${VM_DS_PATH}/${_name}/${_name}.conf" | wc -l | tr -d " ")
        [ "${_newline}" -eq "0" ] && echo "" >> "${VM_DS_PATH}/${_name}/${_name}.conf"
    fi

    sysrc -inqf "${VM_DS_PATH}/${_name}/${_name}.conf" "${_key}=${_value}" >/dev/null 2>&1
}

# remove a value from guest config
config::remove(){
    local _name="$1"
    local _key="$2"

    sysrc -inxqf "${VM_DS_PATH}/${_name}/${_name}.conf" "${_key}" >/dev/null 2>&1
}

# load core configuration file
#
# @modifies VM_CORE_CONFIG VM_CONFIG_USER
#
config::core::load(){
    VM_CONFIG_USER="console;compress;decompress;"

    # check config file exists
    # this is mainly for upgrades to make sure switch/datastore config are migrated
    # DEPRECATED 1.3, remove after
    if [ ! -e "${vm_dir}/.config/system.conf" ]; then
        cat "${vm_dir}/.config/switch" > "${vm_dir}/.config/system.conf" 2>/dev/null
        cat "${vm_dir}/.config/datastore" >> "${vm_dir}/.config/system.conf" 2>/dev/null
    fi

    VM_CORE_CONFIG=$(grep '^[a-z]' "${vm_dir}/.config/system.conf" 2>/dev/null | awk -F# '{print $1}' | sed -e 's@ *$@@' | tr -d '"')
}

# get a value from core config
#
# @param string _c_var variable name to put value into
# @param string _c_key config key to look for
# @param string _c_def default value if not value
# @return 0 if found
#
config::core::get(){
    local _c_var="$1"
    local _c_key="$2"
    local _c_def="$3"
    local _c_line
    local IFS=$'\n'

    for _c_line in ${VM_CORE_CONFIG}; do
        if [ "${_c_key}" = "${_c_line%%=*}" ]; then
            setvar "${_c_var}" "${_c_line#*=}"
            return 0
        fi
    done

    # not found
    setvar "${_c_var}" "${_c_def}"
    return 1
}

# add a value to core configuration
#
# @param string _var variable to set
# @param string _value new value
# @param string _append non-empty to append to existing value
#
config::core::set(){
    local _var="$1"
    local _value="$2"
    local _append="$3"

    if [ -n "${_append}" ]; then
        sysrc -inqf "${vm_dir}/.config/system.conf" "${_var}"+="${_value}" >/dev/null 2>&1
    else
        sysrc -inqf "${vm_dir}/.config/system.conf" "${_var}"="${_value}" >/dev/null 2>&1
    fi
}

# remove a value from core configuration
#
# @param string _var variable to remove
# @param string _value if non-empty we will try to remove just this value from setting
# @return non-zero on error
#
config::core::remove(){
    local _var="$1"
    local _value="$2"

    if [ -n "${_value}" ]; then
        sysrc -inqf "${vm_dir}/.config/system.conf" "${_var}"-="${_value}" >/dev/null 2>&1
    else
        sysrc -inxqf "${vm_dir}/.config/system.conf" ${_var} >/dev/null 2>&1
    fi
}
